package alittlepond;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

import alittlepond.gui.Form;
import alittlepond.gui.Icons;
import alittlepond.gui.WorldWindow;
import alittlepond.world.World;
import alittlepond.world.WorldInitProperty;
import alittlepond.world.WorldProperty;
/**
 * 
 * An initialisation window, shown on first load. 
 * 
 * For now, presents options for creating a new World instance & populating it.
 * 
 * In future, will allow the loading of previous sessions & perhaps other settings will be available.
 * 
 * All actual initialisation is handled by the world in its constructor, be it from parameters or saved data.
 * 
 * If/when init is successful, this window disposes itself and displays the main world window.
 * 
 * @author hopey
 *
 */
@SuppressWarnings("serial")
public class InitWindow extends JFrame{

	public InitWindow() {
		setTitle("A Little Pond");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		
//		add(Box.createVerticalStrut(50));
		
		add(new JLabel("A Little Pond") {{
			setFont(new Font("Serif", Font.BOLD, 20));//d.deriveFont(Font.ITALIC));
			setAlignmentX(0.5f);
			setBorder(BorderFactory.createEmptyBorder(50, 80, 10, 80));
		}});
		
		add(new JLabel("I need to think of a subtiitle thingy.") {{
			setFont(new Font("Serif", Font.ITALIC, 11));
			setAlignmentX(0.5f);
		}});
		
		add(Box.createVerticalStrut(40));
		
		
		
		Form<WorldInitProperty> worldInitForm = new Form<WorldInitProperty>(World.INSTANCE.getInitProps()) ;
		worldInitForm.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createEmptyBorder(10, 10, 10, 10),
				BorderFactory.createEtchedBorder(EtchedBorder.LOWERED)
				));
		add(worldInitForm);
		
		Form<WorldProperty> worldForm = new Form<WorldProperty>(World.INSTANCE) ;
		worldForm.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createEmptyBorder(10, 10, 10, 10),
				BorderFactory.createEtchedBorder(EtchedBorder.LOWERED)
				));
		add(worldForm);
		
		add(new JPanel(){{
			setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
			add(new JButton("Start it", Icons.TICK){{
				addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						startWorld();
					}
				});
				
				
			}});
			add(new JButton("Exit", Icons.CROSS){{
				addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						System.exit(1);
					}
				});
			}});
		}});
		
		
		
		pack();
		
	}
	
	private void startWorld() {
		getContentPane().removeAll();
		
		//when this starts taking a little while, this'll show a loading thingy.
		World.INSTANCE.init();
		
		
		dispose();
		
		
		System.out.println("will show the other one now!");
		WorldWindow worldWindow = new WorldWindow();
		
		worldWindow.init();
		worldWindow.setVisible(true);
	}
	
}
